package com.jia.train.seat.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.extra.pinyin.PinyinUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jia.train.global.enums.BusinessEnum;
import com.jia.train.global.exception.BusinessException;
import com.jia.train.global.page.PageResponse;
import com.jia.train.global.utils.SnowIdUtil;
import com.jia.train.seat.entity.TripStation;
import com.jia.train.seat.entity.TripStationExample;
import com.jia.train.seat.mapper.TripStationMapper;
import com.jia.train.seat.request.TripStationQueryRequest;
import com.jia.train.seat.request.TripStationSaveRequest;
import com.jia.train.seat.response.TripStationQueryResponse;
import com.jia.train.seat.response.TripStationSaveResponse;
import com.jia.train.seat.service.TripStationService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.Duration;
import java.time.LocalTime;
import java.util.List;

@Service
public class TripStationServiceImpl implements TripStationService {

    @Resource
    private TripStationMapper tripStationMapper;

    @Override
    public TripStationSaveResponse insert(TripStationSaveRequest request) {
        TripStation tripStation = BeanUtil.copyProperties(request, TripStation.class);
        DateTime now = DateTime.now();
        String name = request.getName();
        String tripCode = request.getTripCode();
        Integer index = request.getIndex();
        if (ObjectUtil.isNotEmpty(name)) {
            tripStation.setNamePinyin(PinyinUtil.getPinyin(name, ""));
        }
        if (ObjectUtil.isEmpty(tripStation.getId())) {
            tripStation.setId(SnowIdUtil.getLongSnowNextId());
            tripStation.setCreateTime(now);
            if (getTripStationCount(tripCode, index) > 0 || getTripStationCount(tripCode, name) > 0) {
                throw new BusinessException(BusinessEnum.ORDER_OF_THE_SAME_TRAIN_STATION_ALREADY_EXISTS);
            }
            // 时间处理
            LocalTime outTime = request.getOutTime();
            if (LocalTime.MIDNIGHT.equals(outTime)) {
                tripStation.setStopTime(LocalTime.MIDNIGHT);
            } else {
                Duration between = Duration.between(request.getInTime(), request.getOutTime());
                tripStation.setStopTime(LocalTime.MIDNIGHT.plus(between));
            }
            tripStationMapper.insert(tripStation);
        } else {
            tripStation.setUpdateTime(now);
            tripStationMapper.updateByPrimaryKeySelective(tripStation);
        }

        return BeanUtil.copyProperties(tripStation, TripStationSaveResponse.class);
    }

    @Override
    public PageResponse<TripStationQueryResponse> selectList(TripStationQueryRequest request) {

        TripStationExample tripStationExample = new TripStationExample();
        // 排序
        tripStationExample.setOrderByClause("trip_code asc,`index` asc");
        //查询条件
        TripStationExample.Criteria criteria = tripStationExample.createCriteria();
        String tripCode = request.getTripCode();
        if (StringUtils.hasText(tripCode)) {
            criteria.andTripCodeEqualTo(tripCode);
        }

        // 构造分页
        PageHelper.startPage(request.getPage(), request.getSize());
        List<TripStation> tripStations = tripStationMapper.selectByExample(tripStationExample);
        PageInfo<TripStation> pageInfo = new PageInfo<>(tripStations);

        List<TripStationQueryResponse> list
                = BeanUtil.copyToList(tripStations, TripStationQueryResponse.class);

        PageResponse<TripStationQueryResponse> pageResponse = new PageResponse<>();
        pageResponse.setTotal(pageInfo.getTotal());
        pageResponse.setList(list);
        return pageResponse;
    }

    @Override
    public void deleteTripStation(Long id) {
        tripStationMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<TripStation> selectByTripCode(String tripCode) {
        TripStationExample tripStationExample = new TripStationExample();
        tripStationExample.setOrderByClause("`index` asc");
        TripStationExample.Criteria criteria = tripStationExample.createCriteria();
        criteria.andTripCodeEqualTo(tripCode);
        return tripStationMapper.selectByExample(tripStationExample);
    }

    private long getTripStationCount(String code, Integer index) {
        TripStationExample tripStationExample = new TripStationExample();
        TripStationExample.Criteria criteria = tripStationExample.createCriteria();
        criteria.andTripCodeEqualTo(code);
        criteria.andIndexEqualTo(index);
        return tripStationMapper.countByExample(tripStationExample);
    }

    private long getTripStationCount(String code, String name) {
        TripStationExample TripStationExample = new TripStationExample();
        TripStationExample.Criteria criteria = TripStationExample.createCriteria();
        criteria.andTripCodeEqualTo(code);
        criteria.andNameEqualTo(name);
        return tripStationMapper.countByExample(TripStationExample);
    }
}
